package sparrowGui.components
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import sparrowGui.SparrowGUI;
	import sparrowGui.components.item.ItemFactory;
	import sparrowGui.components.item.SButton;
	import sparrowGui.event.AlertEvent;
	import sparrowGui.i.IItem;
	import sparrowGui.i.IItemFactory;
	import sparrowGui.uiStyle.UIStyleMgr;
	
	[Event(name="alert_close", 	type="sparrowGui.event.AlertEvent")]
	[Event(name="alert_update", 	type="sparrowGui.event.AlertEvent")]
	/**
	 * 警告窗
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class SAlert extends BaseUIComponent
	{
		/**
		 * 按钮默认字
		 */
		private static const BTN_DEFAULT_STRING:String = "ok";
		/**
		 * 按钮前缀
		 */
		private static const BTN_PREFIX:String = "btn_prefix$";
//		private var _btnCls:Class;
		
		// 半透明背景
		private var translucent:DisplayObject;
		/**
		 * 父级容器 
		 */
		private var parentSkin:DisplayObjectContainer;
		
		private var _spacing:int = 5;
//		private var _padding:int = 5;
		
		private var _autoClose:Boolean = true;		//是否自动关闭。
		
		/**
		 * 按钮哈希对象<String,IItem>
		 */
		private var btnHash:Object;
		/**
		 * 用于生成按钮的字符数组
		 */
		private var btnStrArr:Array;
		
		/**
		 * 项创建工厂
		 */
		private var btnFactory:IItemFactory;
		
		//提示窗文字文本
		private var txtAlert:TextField;
		//动态按钮坐标
		private var posBtn:DisplayObject;
		//提示输入文本
		public var txtInput:TextField;
		
/*		//提示窗文字文本
		public function get txtAlert():TextField
		{return skin["txtAlert"] as TextField;}
		//提示输入文本
		public function get txtInput():TextField
		{return skin["txtInput"] as TextField;}
		//动态按钮坐标
		public function get posBtn():Sprite
		{return skin["posBtn"] as Sprite;}*/
		/**
		 * 构造提示窗
		 * @param argSkin
		 */
		public function SAlert(parent:DisplayObjectContainer=null,uiVars:Object=null,itemFactory:IItemFactory=null)
		{
			parentSkin = parent || SparrowGUI.mainDisp;
			translucent = newTranslucent();
			btnFactory = itemFactory || new ItemFactory(SButton,"txtItem");
			super(uiVars);
			
			addSkinListen();
		}
		
		override protected function setUI(uiVars:Object=null):void
		{
			super.setUI(uiVars);
			btnHash = {};
			btnStrArr = [BTN_DEFAULT_STRING];
			
			var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
			if(skinDC)
			{
				txtAlert = skinDC.getChildByName("txtAlert") as TextField;
				posBtn = skinDC.getChildByName("posBtn");
			}
			
			embeSkin = false;
		}
		
		/**
		 * 要想新皮肤重新初始设本组件
		 * @param argSkin
		 * @param parent
		 */
		public function reCreate(uiVars:Object=null,parent:DisplayObjectContainer=null):void
		{
			dispose();
			if(parent) parentSkin = parent;
			setUI(uiVars);
			addSkinListen();
		}
		
/*		override protected function create(uiVars:Object=null):void
		{
			super.create(uiVars);
			btnHash = {};
			btnStrArr = [BTN_DEFAULT_STRING];
//			argSkin = argSkin || SkinStyleSheet.getIns().alertSkin;
//			if(_itemClass==null) _itemClass = SkinStyleSheet.getIns().alertBtnCls;
//			if(_itemSkinClass==null) _itemSkinClass = SkinStyleSheet.getIns().alertBtnSkinCls;
			
//			super.create(argSkin);
		}*/
		
		/**
		 * 改变按钮生成工厂
		 * @param newFactory 新的按钮生成工厂
		 */
		public function setBtnFactory(newFactory:IItemFactory):void{
			btnFactory = newFactory;
			updateBtns();
		}
		
		override protected function addSkinListen():void
		{
			super.addSkinListen();
			skin.addEventListener(MouseEvent.CLICK,onBtnClick);
		}
		
		override protected function removeSkinListen():void
		{
			super.removeSkinListen();
			skin.removeEventListener(MouseEvent.CLICK,onBtnClick);
		}
		
		/**
		 * 监听鼠标事件
		 */
		protected function onBtnClick(e:MouseEvent):void
		{
			var btnName:String = e.target.name;
			
			if(btnName.indexOf(BTN_PREFIX)==0){
				var btnIndex:int = int(btnName.replace(BTN_PREFIX,""));
				if(skin.stage) skin.stage.focus = skin.stage;
				var inpTxt:String = (txtInput)?txtInput.text:null;
				
				if(_autoClose) close();
				dispatchEvent(new AlertEvent(AlertEvent.ALERT_CLOSE,btnIndex,inpTxt));
//				if(!_autoClose && _autoHidden) close();
			}
		}
		
		/**
		 * 更新
		 * @param data
		 */
		protected function update(data:Object):void
		{
			if(txtAlert) txtAlert.text = String(data);
			
			dispatchEvent(new AlertEvent(AlertEvent.ALERT_UPDATE));
		}
		
		/**
		 * 弹出警告窗
		 * @param alertTxt
		 * @param btnStr
		 * @param backFun
		 */
		public function show(alertTxt:Object,btnStr:String="确定",backFun:Function=null):void
		{
			if(!btnStr) btnStr = BTN_DEFAULT_STRING;

			btnStrArr = btnStr.split("|");
			updateBtns();
			
			showSkin();
			update(alertTxt);
			
			if(backFun==null) return;
			addEventListener(AlertEvent.ALERT_CLOSE,backFun);
		}
		
		/**
		 * 将皮肤居中显示
		 */
		protected function showSkin():void
		{
			translucent.width = SparrowGUI.stageWidth;
			translucent.height = SparrowGUI.stageHeight;
			parentSkin.addChild(translucent);
			skin.x = SparrowGUI.stageWidth/2 - skin.width/2;
			skin.y = SparrowGUI.stageHeight/2 - skin.height/2;
			parentSkin.addChild(skin);
		}
		
		/**
		 * 跟dispose不同，close只是关掉，并不断开引用
		 */
		public function close():void
		{
			if(translucent.parent)
				translucent.parent.removeChild(translucent);
			
			if(skin.parent)
				skin.parent.removeChild(skin);
			
			removeBtns();
		}
		
		/**
		 * 通过字符数据生成按钮
		 * @param btnStr		用竖线分隔的按钮字"确定|取消"
		 * @param btnCla
		 */
		protected function updateBtns():void
		{
			var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
			btnHash = {};
			
			for (var i:int = 0; i < btnStrArr.length; i++) 
			{
				var itm:IItem = newBtn();
				itm.name = BTN_PREFIX + i;
				itm.update(btnStrArr[i]);
				btnHash[itm.name] = itm;
				
				if(!posBtn) continue;
				var itmSkin:DisplayObject = itm.skin;
				skinDC.addChild(itmSkin);
				var lx:int = itmSkin.width*btnStrArr.length/2 + spacing*(btnStrArr.length-1)/2;
				itmSkin.x = posBtn.x - lx + (itmSkin.width+spacing)*i;
				itmSkin.y = posBtn.y - itmSkin.height;
			}
		}
		
		private function removeBtns():void
		{
			for each (var itm:IItem in btnHash) 
			{
				btnFactory.disposeItemSkin(itm.skin);
				btnFactory.disposeItem(itm);
			}
			btnHash = {};
		}
		
		/**
		 * 创建半透明背景层
		 * @return 
		 */
		protected function newTranslucent():DisplayObject
		{
			return UIStyleMgr.getIns().createSkinUI(getTranslucentDefaultUI());
		}
		
		/**
		 * 从缓存链表中生成item
		 * @return 
		 */
		protected function newBtn(btnSkin:DisplayObject=null):IItem
		{
			btnSkin = btnSkin || newBtnSkin();
			return btnFactory.createNewItem();
/*			var itm:SButton = SparrowGUI.getObjByClassCache(_itemClass);
			var itmSkin:Sprite;
			
			var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
			if(!posBtn && skinDC)
			{
				itmSkin = skinDC.getChildByName(BTN_PREFIX + id) as Sprite;
			}
			else
			{
				itmSkin = itmSkin || SparrowGUI.getObjByClassCache(_itemSkinClass) || new _itemSkinClass();
				itmSkin.name = BTN_PREFIX + id;
			}
			
			if(!itm)
				itm = new _itemClass(itmSkin);
			else
				itm.reset(itmSkin);
			
			itm.update(btnStr);
			btnHash[itm.name] = itm;
			
			if(posBtn)
				itm.addSkinToParent(skinDC);
			
			return itm;*/
		}
		
		/**
		 * 按钮皮肤
		 * @return 
		 */
		protected function newBtnSkin():DisplayObject
		{
			return btnFactory.createNewItemSkin();
		}
		
		///////////////////////////////////
		// get/set
		//////////////////////////////////
		
		
		override public function getDefaultUIName():String
		{
			return "alert";
		}
		
		protected function getTranslucentDefaultUI():String
		{
			return "translucent";
		}

		/**
		 * 断开引用并回收
		 */
		override public function dispose():void
		{
			removeSkinListen();
			close();
			super.dispose();
		}
		
		////////////////////////////////////
		// get/set
		///////////////////////////////////
		
		/**
		 * 子项的Class,必须继承Item
		 * 不想用默认皮肤,可以先设置此基再update数据
		 
		public function get itemClass():Class
		{
			return _itemClass;
		}*/
		
		/**
		 * 子项的Class,必须继承	
		 * 不想用默认皮肤,可以先设置此基再update数据
		 
		public function set itemClass(value:Class):void
		{
			btnFactory.itemClass = value;
		}*/
		
		/**
		 * 子项皮肤类,必须是是Sprite显示对象
		 * 不想用默认皮肤,可以先设置此基再update数据
		 
		public function get itemSkinClass():Class
		{
			return _itemSkinClass;
		}*/
		
		/**
		 * 子项皮肤类,必须是是Sprite显示对象
		 * 不想用默认皮肤,可以先设置此基再update数据
		 
		public function set itemSkinClass(value:Class):void
		{
			btnFactory.itemSkinClass = value;
		}*/

		/**
		 * 边距
		 
		public function get padding():int
		{
			return _padding;
		}*/

		/**
		 * @private
		 
		public function set padding(value:int):void
		{
			_padding = value;
		}*/

		/**
		 * 按钮间距
		 */
		public function get spacing():int
		{
			return _spacing;
		}
		
		/**
		 * 警告窗是否显示
		 */
		public function get isShow():Boolean 
		{
			return skin.parent!=null;
		}

		/**
		 * @private
		 */
		public function set spacing(value:int):void
		{
			_spacing = value;
		}

		public function get autoClose():Boolean
		{
			return _autoClose;
		}

		public function set autoClose(value:Boolean):void
		{
			_autoClose = value;
		}
		
		public function set BtnClass(value:Class):void 
		{
			btnFactory.itemClass = value;
		}
		
		public function get BtnClass():Class 
		{
			return btnFactory.itemClass;
		}
		
		public function set BtnSkinClass(value:Class):void 
		{
			btnFactory.itemClass = value;
		}
		
		public function get BtnSkinClass():Class 
		{
			return btnFactory.itemClass;
		}
	}
}
